import formInfo from '@ohos.app.form.formInfo';
import formBindingData from '@ohos.app.form.formBindingData';
import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
import relationalStore from '@ohos.data.relationalStore';
const tag = 'FormExtensionAbility log==>'
export default class EntryFormAbility extends FormExtensionAbility {
  private rdbStore: relationalStore.RdbStore = null
  async queryCartByID(context){
    this.rdbStore = await relationalStore.getRdbStore(this.context,
      { name: 'product', securityLevel: relationalStore.SecurityLevel.S1 })
    let predicates_cart = new relationalStore.RdbPredicates('form')
    predicates_cart.limitAs(1)
    // 查询
    let rs: relationalStore.ResultSet = await this.rdbStore.query(predicates_cart)
    rs.goToFirstRow()
    let p = new Product(rs.getString(rs.getColumnIndex('icon')),
      rs.getString(rs.getColumnIndex('title')),
      rs.getString(rs.getColumnIndex('desc')), Number(rs.getString(rs.getColumnIndex('priceOld'))),
      Number(rs.getString(rs.getColumnIndex('priceNew'))), rs.getString(rs.getColumnIndex('detailImg1')),
      rs.getString(rs.getColumnIndex('detailImg2')),
      Number(rs.getString(rs.getColumnIndex('id'))))
    rs.close()
    return p
  }
  onAddForm(want) {
    let title = ''
    this.queryCartByID(this.context)
      .then((product:Product)=>{
        title = product.title
        // detailImg1:cart.
        // detailImg2:cart.title
        console.log(tag+title)
        let formData = {
          'title':title
        };
        return formBindingData.createFormBindingData(formData);
      })
    // Called to return a FormBindingData object.
    let formData = {};
    return formBindingData.createFormBindingData(formData);
  }

  onCastToNormalForm(formId) {
    // Called when the form provider is notified that a temporary form is successfully
    // converted to a normal form.
  }

  onUpdateForm(formId) {
    // Called to notify the form provider to update a specified form.
  }

  onChangeFormVisibility(newStatus) {
    // Called when the form provider receives form events from the system.
  }

  onFormEvent(formId, message) {
    // Called when a specified message event defined by the form provider is triggered.
  }

  onRemoveForm(formId) {
    // Called to notify the form provider that a specified form has been destroyed.
  }

  onAcquireFormState(want) {
    // Called to return a {@link FormState} object.
    return formInfo.FormState.READY;
  }
};
export class Product {
  icon:string // 图片
  title:string // 标题
  desc:string // 描述
  priceOld: number // 原价
  priceNew: number // 现价
  id?:number
  detailImg1:string
  detailImg2:string

  constructor(icon:string, title:string, desc:string, priceOld: number, priceNew?: number,detailImg1?:string,detailImg2?:string,id?:number) {
    this.icon = icon
    this.title = title
    this.desc = desc
    this.priceOld = priceOld
    this.priceNew = priceNew
    this.id = id
    this.detailImg1 = detailImg1
    this.detailImg2 = detailImg2
  }


}